package com.tqjc.area.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tqjc.area.entity.Roller;
import com.tqjc.area.entity.SteelType;
import com.tqjc.area.service.SteelTypeService;
import com.tqjc.area.wrapper.RollerWrapper;
import com.tqjc.area.wrapper.SteelTypeWrapper;
import com.tqjc.system.core.annotation.Log;
import com.tqjc.system.core.model.AjaxJson;
import com.tqjc.system.core.model.LogType;
import com.tqjc.system.core.model.PageInfo;
import com.tqjc.system.core.query.QueryUtils;
import com.tqjc.system.core.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/steelType")
@Slf4j
public class SteelTypeController {

    @Resource
    SteelTypeService steelTypeService;


    //辊道管理主页
    @RequestMapping("/index")
    public String index() {
        return "baotou-steel/AreaManage/SteelTypeManage.html";
    }


    //辊道增加页面主页

    /**
     * 跳转到页面
     *
     * @param rowInfo
     * @param model
     * @return
     */
    @RequestMapping("/add")
    public String add(SteelType rowInfo, Model model) {

        if (rowInfo.getId() != null) {
            SteelType steelType = steelTypeService.queryById(rowInfo.getId());
            model.addAttribute("steelType", steelType);
        }
        return "baotou-steel/AreaManage/SteelTypeManageAdd.html";
    }

    /**
     * 获取表格数据 树形结构
     *
     * @param
     * @param request
     * @return
     */
    @RequestMapping("/datagrid")
    @ResponseBody
    public void datagrid(SteelType steelType, PageInfo<SteelType> page, HttpServletRequest request, HttpServletResponse response) {
        //拼接查询条件
        QueryWrapper<SteelType> queryWrapper = QueryUtils.installQueryWrapperRow(steelType, request.getParameterMap());

        //执行查询
        IPage<SteelType> lstResult = steelTypeService.page(page.getPageEntity(), queryWrapper);

        //结果处理,直接写到客户端
        ResponseUtil.write(response, new SteelTypeWrapper(lstResult).wrap());
    }


    /**
     * 保存方法
     *
     * @param rowInfo
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    @Log(type = LogType.save, name = "钢板型号表", memo = "新增或编辑保存了钢板型号表信息")
    public AjaxJson save(SteelType rowInfo) {
        AjaxJson j = new AjaxJson();

        try {
            //新增保存
            if (rowInfo.getId() == null) {
                //
                steelTypeService.insert(rowInfo);
            }
            //编辑保存
            else {
                QueryWrapper<SteelType> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("id", rowInfo.getId());
                steelTypeService.update(rowInfo, queryWrapper);
//                rollerService.saveOrUpdate(rowInfo);
//                rollerService.update(rowInfo);
            }

        } catch (Exception e) {
            log.error("保存钢板型号信息，错误信息:" + e.getMessage(),e);
            j.setSuccess(false);
            e.printStackTrace();
        }

        return j;
    }


    /**
     * 删除操作
     *
     * @param rowInfo
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    @Log(type = LogType.del, name = "钢板型号表", memo = "删除了钢板型号表信息")
    public AjaxJson delete(SteelType rowInfo) {
        AjaxJson j = new AjaxJson();
        try {
            steelTypeService.deleteById(rowInfo.getId());

        } catch (Exception e) {
            log.error("删除辊道表出错，错误信息:" + e.getMessage(),e);
            j.setSuccess(false);
            e.printStackTrace();
        }

        return j;
    }
}
